<template>
  <div class="material" :id="'Material' + singleMaterial.id">
    <header>
      <section class="material-name">
        <van-checkbox  v-model="singleMaterial.checked" checked-color="#548AF1" style="margin-right:5px;" @click="handleCheckedChange(singleMaterial.checked)" v-if="showCheckBox"></van-checkbox>
        <span>{{ index + 1 }}</span>
        <span class="line"></span>
        <span>{{ singleMaterial.materialEntity&&singleMaterial.materialEntity.name }}</span>
      </section>
      <section
        v-show="singleMaterial.materialEntity&&singleMaterial.materialEntity.isBatchMag"
        class="batch-label"
      >
        <img src="@/assets/images/batch.png" alt="" />
        <div>批次</div>
      </section>
    </header>
    <main>
      <section class="material-code">
        <span>{{ singleMaterial.materialEntity&&singleMaterial.materialEntity.code }} </span>
        <section class="warehouse-num">
          <!-- <section class="num1">
            <span>入库数</span>
            <span>{{ singleMaterial.allPalnNum || 0 }}</span>
          </section>
          <section class="num2">
            <span>入库批次</span>
            <span>{{ singleMaterial.stockMaterialDetaiList && singleMaterial.stockMaterialDetaiList.length ||0}}</span>
          </section> -->
          <section class="num1">
            <span>计划入库数</span>
            <!-- <span>{{ singleMaterial.stockMaterialDetaiList?singleMaterial.stockMaterialDetaiList.reduce((acc, cur) => acc + Number(cur.planNum || 0), 0):0}}</span> -->
            <span>{{ singleMaterial.planAmount }}</span>
          </section>
          <section class="num2">
            <span v-if="tabState==1 || tabState == 3">待入库数</span>
            <span v-else>已入库数</span>
            <span >{{singleMaterial.actualAmount < 0 ? 0: singleMaterial.actualAmount}}</span>
            <!-- <span v-else>{{ singleMaterial.stockMaterialDetaiList?singleMaterial.stockMaterialDetaiList.reduce((acc, cur) => acc +  Number(cur.realNum || 0), 0):0}}</span> -->
            <!-- <span v-else>{{singleMaterial.stockMaterialDetaiList && singleMaterial.stockMaterialDetaiList.length ||0}}</span> -->
          </section>
        </section>
      </section>
      <section class="material-info">
        <section class="sku" v-if="singleMaterial.skuName">
          <label>特征参数：</label>
          <!-- <el-input
            disabled
            size="mini"
            v-model="singleMaterial.skuName"
          ></el-input> -->
          <span style="font-size: 12px;">{{singleMaterial.skuName}}</span>
        </section>

        <section class="sku">
          <label>规格：</label>
          <!-- <el-input
            disabled
            size="mini"
            :value="singleMaterial.materialEntity?singleMaterial.materialEntity.standard:''"
          ></el-input> -->
          <span style="font-size: 12px;">{{singleMaterial.materialEntity?singleMaterial.materialEntity.standard:''}}</span>
        </section>
        <section class="sku" style="margin:5px 0">
          <div style="width:50%"><label>物料单位：</label><span style="font-size: 12px;">{{singleMaterial.materialEntity?singleMaterial.materialEntity.comp:''}}</span></div>
          <div style="width:50%" v-if="singleMaterial.materialEntity && singleMaterial.materialEntity.isDoubleUnit"><label>计量单位：</label><span style="font-size: 12px;">{{singleMaterial.materialEntity?singleMaterial.materialEntity.unit:''}}</span></div>
        </section>
        <section class="sku" v-if="!singleMaterial.materialEntity.isBatchMag">
          <label>实际数量：</label>
          <el-input size="mini" v-model="singleMaterial.realNum" readonly :disabled="singleMaterial.materialEntity.isBatchMag || infoData.state >1"
            @click.native="realNumDialog"> </el-input>
        </section>
        <section class="sku">
          <label>备注：</label>
          <el-input
            :disabled="tabState!=3"
            size="mini"
            v-model="singleMaterial.materialRemark"
          ></el-input>
        </section>
        <section class="sku">
          <label>供应商：</label>
          <!-- <el-input
            disabled
            size="mini"
            :value="singleMaterial.materialEntity?singleMaterial.materialEntity.standard:''"
          ></el-input> -->
          <span style="font-size: 12px;">{{ singleMaterial.supplierName }}</span>
        </section>
        
        <!-- <section
          v-show="
            singleMaterial.actualAmount &&
            singleMaterial.actualAmount - singleMaterial.planAmount > 0
          "
          class="line-tip"
        >
          <section style="color: red">
            <span>提示：</span>
            <span>实际数量已超出计划数量</span>
          </section>
        </section>
        <section class="line">
          <section>
            <label>物料类型：</label>
            <el-input
              size="mini"
              disabled
              v-model="singleMaterial.materialFields.typeName"
            ></el-input>
          </section>
          <section class="item-right">
            <label>行备注：</label>

            <section class="line-remark">
              <el-input
                size="mini"
                v-model="singleMaterial.materialRemark"
              ></el-input>
              <section @click="materialRemarkFocus" class="mask"></section>
            </section>
          </section>
        </section> -->
      </section>
      <div v-show="singleMaterial.materialEntity.isBatchMag">
        <materialtable
          ref="materialtable"
          style="margin-top:12px;"
          :materialEntity="singleMaterial.materialEntity || {}"
          v-show="singleMaterial.isExpand"
          :tableData="singleMaterial.stockMaterialDetaiList"
          :tabState="tabState"
          @numDialog="numDialog"
        />
      </div>

      <!-- <section
        v-show="
          singleMaterial.isExpand &&
          singleMaterial.stockMaterialDetaiList.length
        "
        class="batchs"
      >
        <section class="table-header">
          <section>
            <span>批次号</span>
            <span>{{
              `(${
                singleMaterial.stockMaterialDetaiList.filter(
                  i => i.state === 2 || i.state === 3
                ).length
              }/${singleMaterial.stockMaterialDetaiList.length})`
            }}</span>
          </section>

          <span>库位</span>
          <span>计划退料数</span>
          <span>实际退料数</span>
        </section>
        <div class="">

        </div> -->
        <!-- <ul class="table-body">
          <li
            v-for="(batch, index) in singleMaterial.stockMaterialDetaiList"
            :key="index"
            class="tr"
            :style="{
              backgroundColor:
                batch.state === 1
                  ? '#FEFCEE'
                  : batch.state === 2
                  ? '#BBFFB8'
                  : '#eeeeee'
            }"
          >
            <section>
               <img
                v-if="batch.state === 2"
                src="@/assets/images/sure.png"
                alt=""
              />
              <img
                v-if="batch.state === 1"
                src="@/assets/images/recommend.png"
                alt=""
              />
              <span>
                <van-checkbox shape="square" icon-size="12px" v-model="batch.checked" checked-color="#548AF1" style="margin-right:5px;"></van-checkbox>
              </span>

              <div>{{ batch.stockInventoryDetailEntity.barCode }}</div>
            </section>

            <span>{{
              batch.stockInventoryDetailEntity.stockLocationName
            }}</span>
            <span>{{ batch.realNum }}</span>
            <span>{{ batch.realNum }}</span>
          </li>
        </ul>
      </section>-->
    </main>

    <footer>
      <section class="btns">

        <section
          @click="handleExpandClick()"
          class="expand-fold"
        >
          <span>{{ singleMaterial.isExpand ? '收起' : '展开' }}</span>
          <img
            v-if="singleMaterial.isExpand"
            style="width: 11px"
            src="@/assets/images/up-icon.png"
            alt=""
          />
          <img
            style="width: 11px"
            v-else
            src="@/assets/images/down-icon.png"
            alt=""
          />
        </section>
        <section class="action">
          <slot name="action"> </slot>
        </section>
      </section>
    </footer>
    <van-dialog
      v-model="showRealNum"
      title="实际数量"
      show-cancel-button
      confirm-button-color="#548AF1"
      :before-close="beforClose"
    >
      <van-form ref="realNumForm" @submit.native.prevent label-width="130px">
        <van-field
          name="realNum"
          v-model="realNum"
          type="number"
          label="实际数量"
          :rules="getRealNum()"
          @change="calcRealNum = realNumChange(realNum)"
        />
        <van-field
          v-integer-only
          v-if="singleMaterial.materialEntity.isDoubleUnit"
          name="realNum"
          v-model="calcRealNum"
          type="number"
          label="实际数量(计量单位)"
          @change="calcRealNumChange"
        />
      </van-form>
    </van-dialog>
  </div>
</template>

<script>
import materialtable from "./materialtable";
import Decimal from "decimal.js";
export default {
  components: {materialtable},
  props: {
    singleMaterial: {
      type: Object,
      default: () => ({})
    },
    index: {
      type: Number
    },
    tabState:{
      type: [Number,String]
    },
    infoData:{
      type:Object,
      default: () => ({})
    },
    showCheckBox: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      showRealNum:false,
      realNum:0,
      calcRealNum:""
    }
  },
  watch:{
    singleMaterial:{
      handler(n,o){
        console.log(this.singleMaterial,'----=======')
        const numSum = n.stockMaterialDetaiList.reduce(
          (accumulator, currentValue) => {
            if(this.tabState == 1){
              return Decimal(accumulator||0).add(Decimal(currentValue.planNum||0));
            }else{
              return Decimal(accumulator||0).add(Decimal(currentValue.realNum||0));
            }
          },
          0
        );
        this.$set(n,'allPalnNum',numSum.valueOf())
      },
      deep:true,
      immediate:true
    },
    'singleMaterial.stockMaterialDetaiList':{
      handler(n,o){
        if(n.length == 0){
          return
        }
        const areAllCheckedTrue = n.every(subItem => {
          return subItem &&subItem.checked === true
        });
        if(areAllCheckedTrue){
          this.singleMaterial.checked = true
        }else{
          this.singleMaterial.checked = false
        }

      },
      deep:true
    }
  },
  methods: {
    realNumChange(val){
      if(this.singleMaterial.materialEntity.isDoubleUnit && this.singleMaterial.materialEntity.scaleFactor){
        return  Math.floor(Number((val/(this.singleMaterial.materialEntity.scaleFactor||0)).toFixed(4)))
      }
      return ''
    },
    calcRealNumChange(val){
      if(this.singleMaterial.materialEntity.isDoubleUnit && this.singleMaterial.materialEntity.scaleFactor){
        this.realNum = Number((this.calcRealNum*(this.singleMaterial.materialEntity.scaleFactor)).toFixed(4))
      }
    },
     realNumDialog(){
      if(this.tabState != 3){
        return
      }
      if(this.singleMaterial.materialEntity.isBatchMag){
        return
      }
      this.showRealNum = true
      this.realNum = JSON.parse(JSON.stringify(this.singleMaterial)).realNum
      if(this.singleMaterial.materialEntity.isDoubleUnit && this.singleMaterial.materialEntity.scaleFactor){
        this.calcRealNum = Math.floor(Number((this.realNum/(this.singleMaterial.materialEntity.scaleFactor||0)).toFixed(4)))
        // this.calcPlanNum = Number((this.currentRow.planNum/(this.materialEntity.scaleFactor||0)).toFixed(4))
      }
    },
    getRealNum(item) {
      return [
        {
          item,
          validator: this.validatorRealNum,
          message: "实际数量不能为空,不能小于等于0",
          trigger: "onBlur",
        },
      ];
    },
    validatorRealNum(val, rule) {
      if (!val) {
        return false;
      }
      if (val <= 0) {
        return false;
      } else {
        return true;
      }
    },
    async beforClose(action,done){
      if(action == 'confirm'){
        this.$refs.realNumForm
        .validate("realNum")
        .then(async (res) => {
          this.singleMaterial.realNum = this.realNum
          if(this.singleMaterial.stockMaterialDetaiList && this.singleMaterial.stockMaterialDetaiList.length>0){
            this.singleMaterial.stockMaterialDetaiList[0].realNum = this.realNum
          }

          done()
        }).catch(()=>{
          done(false)
        })

      }else{
        done()
      }
    },
    handleCheckedChange(val){
      if(val){
        this.singleMaterial.stockMaterialDetaiList.map(item=>item.checked = true)
      }else{
        this.singleMaterial.stockMaterialDetaiList.map(item=>item.checked = false)
      }
      // console.log(this.singleMaterial.stockMaterialDetaiList)
      this.singleMaterial.stockMaterialDetaiList.splice()
    },
    materialRemarkFocus() {
      this.$emit('updateMaterialRemark', this.singleMaterial)
    },
    handleExpandClick(){
      this.singleMaterial.isExpand = !this.singleMaterial.isExpand
      this.$emit('updateMaterialExpand', this.singleMaterial)
    },
    numDialog(){
      this.$emit('numDialog')
    }
  }
}
</script>

<style lang="scss" scoped>
.material {
  background: #ffffff;
  box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.16);
  border-radius: 4px 4px 4px 4px;
  margin-bottom: 8px;
  header {
    background: linear-gradient(90deg, #DCE8FF 0%, rgba(255,255,255,0) 100%);
    border-radius: 4px 4px 0px 0px;
    height: 38px;
    display: flex;
    justify-content: space-between;
    .material-name {
      padding-left: 11px;
      font-size: 18px;
      font-family: Source Han Sans CN, Source Han Sans CN;
      font-weight: 800;
      color: #333333;
      display: flex;
      align-items: center;
      .line {
        margin: 0 12px 0 17px;
        display: inline-block;
        width: 1px;
        height: 25px;
        background: linear-gradient(
          180deg,
          rgba(255, 255, 255, 0) 0%,
          #9f9f9f 54%,
          rgba(236, 255, 239, 0) 100%
        );
        border-radius: 0px 0px 0px 0px;
        opacity: 1;
      }
      & > span:last-child {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display: inline-block;
        width: 200px;
      }
    }
    .batch-label {
      position: relative;
      div {
        position: absolute;
        // width: 50%;
        // height: 134%;
        -webkit-transform: rotate(50deg);
        transform: rotate(50deg);
        font-size: 12px;
        font-family: SourceHanSansCN-Regular;
        color: #fff;
        top: 7px;
        right: 2px;
      }
    }
  }
  main {
    padding: 0 5px 7px 5px;
    .material-code {
      height: 32px;
      display: flex;

      justify-content: space-between;
      align-items: center;
      & > span {
        font-size: 16px;
        font-family: Source Han Sans CN, Source Han Sans CN;
        font-weight: 500;
        color: #333333;
      }
      .warehouse-num {
        display: flex;
        .num1,
        .num2 {
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          span:nth-of-type(1) {
            font-size: 10px;
            font-family: Source Han Sans CN, Source Han Sans CN;
            font-weight: 400;
            color: #333333;
          }
          span:nth-of-type(2) {
            font-size: 12px;
            font-family: Source Han Sans CN, Source Han Sans CN;
            font-weight: 400;
          }
        }
        .num1 {
          width: 64px;
          height: 32px;
          background: rgba(255, 242, 223, 0.52);
          border-radius: 2px 2px 2px 2px;
          opacity: 1;
          border: 1px solid #fbb240;
          margin-right: 4px;
          span:nth-of-type(2) {
            color: #f29a21;
          }
        }
      }
      .num2 {
        width: 64px;
        height: 32px;
        background: #e7ffe6;
        border-radius: 2px 2px 2px 2px;
        opacity: 1;
        border: 1px solid #48ba47;
        span:nth-of-type(2) {
          color: #48ba47;
        }
      }
    }
    .material-info {
      label {
        font-size: 14px;
        font-family: SourceHanSansCN-Regular, SourceHanSansCN-Regular;
        font-weight: normal;
        color: #666666;
      }
      .sku {
        display: flex;
        margin-top: 4px;
        .el-input {
          // width: 16.4375rem;
          ::v-deep{
            .el-input__inner{
              // width: 16.4375rem;
            }
          }

        }
        label{
          display: inline-block;
          width: 6rem;
        }
      }

      .line {
        display: flex;
        justify-content: space-between;
        &:nth-of-type(2) {
          margin: 4px 0;
        }
        .el-input {
          width: 73px;
        }

        .item-right {
          display: flex;
          align-items: center;
          .line-remark {
            position: relative;
            .mask {
              position: absolute;
              top: 0;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .line-tip {
        color: red;
        font-size: 14px;
        margin-bottom: 4px;
        span:nth-of-type(2) {
          font-size: 13px;
        }
      }
    }
    .batchs {
      //   padding: 10px 11px 0 9px;
      margin-top: 10px;

      .table-header {
        font-size: 13px;
        font-family: Source Han Sans CN, Source Han Sans CN;
        font-weight: 400;
        color: #666666;
        display: flex;
        background: #f6f6f6;
        border-radius: 2px 2px 0px 0px;
        height: 28px;
        line-height: 28px;
        & > section {
          width: 25%;
          padding-left: 20px;
          & > span:nth-of-type(2) {
            font-weight: bold;
            color: #666666;
          }
        }
        & > span {
          display: inline-block;
          width: 25%;
        }
        & > span:nth-of-type(2) {
          padding-left: 8px;
        }
        & > span:nth-of-type(3) {
          padding-left: 8px;
        }
      }
      .table-body {
        .tr {
          font-size: 13px;
          font-family: Source Han Sans CN, Source Han Sans CN;
          font-weight: 400;
          color: #666666;
          display: flex;
          background: #f6f6f6;
          border-radius: 2px 2px 0px 0px;
          min-height: 28px;
          line-height: 28px;
          align-items: center;

          & > section {
            width: 40%;
            display: flex;
            align-items: center;
            & > div {

              font-weight: bold;
              color: #666666;
              // margin-left: 5px;
              word-wrap:break-word
            }
            & > span:nth-child(1) {
              margin-left: 5px;
            }
          }

          & > span:nth-of-type(1),
          & > span:nth-of-type(2),
          & > span:nth-of-type(3) {
            width: 20%;
          }
          span:nth-of-type(2),
          span:nth-of-type(3) {
            padding-left: 8px;
          }
        }
      }
    }
  }
  footer {
    height: 45px;
    padding: 0 7px 0 4px;
    .btns {
      border-top: 1px solid #ebebeb;
      height: 100%;
      display: flex;
      justify-content: space-between;
      padding: 0 5;
      .expand-fold {
        display: flex;
        align-items: center;
        & > span {
          font-size: 14px;
          font-family: Source Han Sans CN, Source Han Sans CN;
          font-weight: 400;
          color: #999999;
        }
        img {
          margin-left: 6px;
        }
      }
      .action {
        display: flex;
        align-items: center;
        .el-button {
          // border: 1px solid #548af1;
          // padding: 6px 6px 8px 6px;
          padding: 0;
          width: 64px;
          height: 32px;
          line-height: 32px;
          text-align: center;
        }
      }
    }
  }
}
</style>
